package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysContract;
import com.ruoyi.system.domain.SysContractLabel;
import com.ruoyi.system.domain.SysContractLog;
import com.ruoyi.system.service.ISysContractLogService;
import com.ruoyi.system.service.ISysContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysContractPayLabelMapper;
import com.ruoyi.system.domain.SysContractPayLabel;
import com.ruoyi.system.service.ISysContractPayLabelService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * 合同付费列Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-17
 */
@Service
public class SysContractPayLabelServiceImpl implements ISysContractPayLabelService
{
    @Autowired
    private SysContractPayLabelMapper sysContractPayLabelMapper;

    /** 合同service接口层 */
    @Autowired
    private ISysContractService sysContractService;

    /** 合同日志service接口层 */
    @Autowired
    private ISysContractLogService sysContractLogService;

    /**
     * 查询合同付费列
     *
     * @param id 合同付费列主键
     * @return 合同付费列
     */
    @Override
    public SysContractPayLabel selectSysContractPayLabelById(Long id)
    {
        return sysContractPayLabelMapper.selectSysContractPayLabelById(id);
    }

    /**
     * 根据合同主键查询合同付费列
     * @param contractId
     * @return
     */
    @Override
    public List<SysContractPayLabel> selectSysContractPayLabelByContractId(Long contractId) {
        return sysContractPayLabelMapper.selectSysContractPayLabelByContractId(contractId);
    }

    /**
     * 查询合同付费列列表
     *
     * @param sysContractPayLabel 合同付费列
     * @return 合同付费列
     */
    @Override
    public List<SysContractPayLabel> selectSysContractPayLabelList(SysContractPayLabel sysContractPayLabel)
    {
        return sysContractPayLabelMapper.selectSysContractPayLabelList(sysContractPayLabel);
    }

    /**
     * 新增合同付费列
     *
     * @param sysContractPayLabel 合同付费列
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysContractPayLabel(SysContractPayLabel sysContractPayLabel)
    {
        int status = 0;
        try {
            //新增合同付费列，判断方法
            emptyInsertSysContractPayLabel(sysContractPayLabel);

            //构建实体
            sysContractPayLabel = buildSysContractPayLabel(sysContractPayLabel);

            //修改合同剩余金额
            sysContractService.updateSysContractRemainingMoney(sysContractPayLabel.getContractId(),sysContractPayLabel.getPayMoney(),false);

            //修改支付金额(已支付)
            if (sysContractPayLabel.getIsPay() == 1){
                sysContractService.updateSysContractPayMoney(sysContractPayLabel.getContractId(),sysContractPayLabel.getPayMoney(),true);
            }

            //新增方法
            status = sysContractPayLabelMapper.insertSysContractPayLabel(sysContractPayLabel);

            //构建合同日志
            SysContractLog sysContractLog = sysContractLogService.buildSysContractLog(sysContractPayLabel.getContractId(), "修改付费日志");

            //新增合同日志
            sysContractLogService.insertSysContractLog(sysContractLog);
        }
        catch (Exception e){
            //事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new RuntimeException(e.getMessage());
        }
        return status;
    }

    /**
     * 构建合同付费列
     * @return
     */
    @Override
    public SysContractPayLabel buildSysContractPayLabel(SysContractPayLabel sysContractPayLabel) {
        //创建时间
        sysContractPayLabel.setCreateTime(DateUtils.getNowDate());
        //创建人
        //sysContractPayLabel.setCreateBy(SecurityUtils.getUsername());
        sysContractPayLabel.setCreateBy("admin");
        return sysContractPayLabel;
    }

    /**
     * 新增合同付费列判断方法
     * @param sysContractPayLabel
     */
    @Override
    public void emptyInsertSysContractPayLabel(SysContractPayLabel sysContractPayLabel) {
        //合同主键
        if (null == sysContractPayLabel.getContractId()){
            throw new RuntimeException("合同主键未传输，请联系管理员");
        }
        //类型
        if (null == sysContractPayLabel.getPayType()){
            throw new RuntimeException("请选择类型");
        }
        //支付金额
        if (null == sysContractPayLabel.getPayMoney() || sysContractPayLabel.getPayMoney().compareTo(new BigDecimal(0)) == 0){
            throw new RuntimeException("支付金额不可为空或为0");
        }
        //支付方式
        if (null == sysContractPayLabel.getPayMoney()){
            throw new RuntimeException("请选择支付方式");
        }
    }

    /**
     * 修改合同付费列
     *
     * @param sysContractPayLabel 合同付费列
     * @return 结果
     */
    @Override
    public int updateSysContractPayLabel(SysContractPayLabel sysContractPayLabel)
    {
        return sysContractPayLabelMapper.updateSysContractPayLabel(sysContractPayLabel);
    }

    /**
     * 批量删除合同付费列
     *
     * @param ids 需要删除的合同付费列主键
     * @return 结果
     */
    @Override
    public int deleteSysContractPayLabelByIds(Long[] ids)
    {
        return sysContractPayLabelMapper.deleteSysContractPayLabelByIds(ids);
    }

    /**
     * 删除合同付费列信息
     *
     * @param id 合同付费列主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysContractPayLabelById(Long id)
    {
        int status = 0;
        try {
            //查询付费合同列
            SysContractPayLabel sysContractPayLabel = this.selectSysContractPayLabelById(id);
            if (null == sysContractPayLabel){
                throw new RuntimeException("未查询到付费合同信息");
            }
            //查询合同
            SysContract sysContract = sysContractService.selectSysContractById(sysContractPayLabel.getContractId());
            if (null == sysContract){
                throw new RuntimeException("未查询到合同");
            }

            //判断状态是否可删除
            if (sysContract.getContractConfirmStatus() != 0 ){
                throw new RuntimeException("当前状态不可删除付费列表");
            }

            //修改剩余金额
            sysContractService.updateSysContractRemainingMoney(sysContract.getId(),sysContractPayLabel.getPayMoney(),true);

            //修改支付金额(已支付)
            if (sysContractPayLabel.getIsPay() == 1){
                sysContractService.updateSysContractPayMoney(sysContract.getId(),sysContract.getPayMoney(),false);
            }
            //修改支付金额(未支付)
            /*else {
                sysContractService.updateSysContractPayMoney(sysContract.getId(),sysContract.getPayMoney(),false);
            }*/

            status = sysContractPayLabelMapper.deleteSysContractPayLabelById(id);

            //构建合同日志
            SysContractLog sysContractLog = sysContractLogService.buildSysContractLog(sysContract.getId(),"删除支付日志");

            //新增合同日志
            sysContractLogService.insertSysContractLog(sysContractLog);
        }
        catch (Exception e){
            //事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new RuntimeException(e.getMessage());
        }
        return status;
    }

    /**
     * 根据合同主键删除合同付费明细
     * @param contractId
     * @return
     */
    @Override
    public int deleteSysContractPayLabelByContractId(Long contractId) {
        return sysContractPayLabelMapper.deleteSysContractPayLabelByContractId(contractId);
    }
}